package com.yiren.manager.ctrl.beds;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.StringUtil;
import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrRoom;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.beds.YrRoomForm;
import com.yiren.manager.service.iface.beds.YrRoomService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("room")
@Controller
public class YrRoomController extends GenericController<YrRoomForm, YrRoom>{
	
	@Autowired
	private YrRoomService yrRoomService;
	
	//保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrRoomForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrRoomForm> result = new HTTPResult<YrRoomForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrRoomForm form = tool.mapper("room", YrRoomForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if(null==form.getRoomId()||form.getRoomId().isEmpty()){
			form.setRoomId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());
		YrRoom entity = ObjectUtils.objectToObject(form, YrRoom.class);
		yrRoomService.save(entity);
		
		return result;
	}
	
	/**获取所有记录*/
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrRoomForm> findAll() throws MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrRoomForm> result = new HTTPResult<YrRoomForm>();
		
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put("roomNumber", "ASC");
		
		QueryResult<YrRoom> qr = yrRoomService.find(YrRoom.class, null, new Object[]{},map);
		List<YrRoom> entityList = qr.getResultList();
		List<YrRoomForm> formList = new ArrayList<YrRoomForm>();
		for(YrRoom room : entityList){
			YrRoomForm form = ObjectUtils.objectToObject(room, YrRoomForm.class);
			formList.add(form);
		}
		result.setData(formList);
		return result;
	}
	
	/**删除一条记录*/
	@RequestMapping("delete")
	@ResponseBody
	public HTTPResult<YrRoomForm> delete(String id) throws MyException{
		HTTPResult<YrRoomForm> result = new HTTPResult<YrRoomForm>();
		if(StringUtil.isEmpty(id)){
			throwExp("param.error");
		}
		yrRoomService.delete(id);
		return result;
	}
	
	/**
	 * 根据楼房id,楼层id获取房间列表
	 * @param buildingId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("find")
	@ResponseBody
	public HTTPResult<YrRoomForm> find(String floorId) throws Exception{
		HTTPResult<YrRoomForm> result = new HTTPResult<YrRoomForm>();
		if(null==floorId){
			throwExp("param.error");
		}
		
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put("roomNumber", "ASC");
		
		String sqlWhere = "floor_id=?1";
		QueryResult<YrRoom> qr =  yrRoomService.find(YrRoom.class, sqlWhere, new Object[]{floorId},map);
		
		List<YrRoomForm> formList = new ArrayList<YrRoomForm>();
		for(YrRoom room : qr.getResultList()){
			formList.add(toForm(room));
		}
		result.setData(formList);
		return result;
	}
	
	/**获取一条记录*/
	@RequestMapping("findOne")
	@ResponseBody
	public HTTPResult<YrRoomForm> findOne(String id) throws MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrRoomForm> result = new HTTPResult<YrRoomForm>();
		if(null==id){
			throwExp("param.error");
		}
		YrRoom entity = yrRoomService.findOne(id);
		List<YrRoomForm> formList = new ArrayList<YrRoomForm>();
		formList.add(ObjectUtils.objectToObject(entity, YrRoomForm.class));
		result.setData(formList);
		return result;
	}
	
	/**
	 * 批量删除
	 * @param data
	 * @return
	 * @throws SwallowException
	 * @throws MyException
	 */
	@RequestMapping("deleteBatch")
	@ResponseBody
	public HTTPResult<YrRoomForm> deleteBatch(String data) throws SwallowException, MyException{
		HTTPResult<YrRoomForm> result = new HTTPResult<YrRoomForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		String[] ids = tool.mapper("ids", String[].class);
		for(String id : ids){
			yrRoomService.delete(id);
		}
		return result;
	}
	
	@Override
	public YrRoomForm toForm(YrRoom e) throws Exception {
		YrRoomForm form = ObjectUtils.objectToObject(e, YrRoomForm.class);
		return form;
	}

	@Override
	public YrRoom toEntity(YrRoomForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
